#!/usr/bin/python
from Parser import *
from User import *
from NormalNames import *
from Config import *
class Stats(Config):
	You = User()
	Logfile = ""
	Data = []
	def __init__(self,Nick,Logfile):
		Pr = Parser(Logfile);
		Pr.doParse()
		self.Data = Pr.Lines
		self.You.Name = Nick
	def exceptDeath(self,Value):
                Mn = NormalNames()
		F = 0
                try:
                    if self.Exc.index(Mn.getClears(Value))>-1:
                        F = 1
                except ValueError:
                    A = 0
		return F
	def searchNick(self,Data):
		Mn = NormalNames()
		for i in range(len(Data)):
			if i!=0 and i!=1 and i!=3 and Mn.getClears(Data[1])[:5]!="[BOT]"  :
				if Data[i][0:len(self.You.Name)]==self.You.Name and Data[1]!="Connected" and Data[0][0:7]!="console":
					return 1
		return 0
	def detectHitZone(self,Zone):
		if len(Zone)>5:
			for i in range(len(self.Zones)):
				if self.Zones[i] == Zone[5][:len(self.Zones[i])]:
					return i
		return -1
	def detectFlagAction(self,FlagAct):
		if len(FlagAct)>3:
			for i in range(len(self.Actions)):
				if self.Actions[i] == FlagAct[1] or self.Actions[i] == FlagAct[2] or self.Actions[i] == FlagAct[3]:
					return i		
		return -1
        def checknick(self,Name,Type):
                User = "[PUSTO]"
                F = 0
                Mn = NormalNames()
                if Type == 0:
                    for i in range(2,len(Name)):
                               for w in self.DLabels:
                                    if w ==Mn.getClears(Name[i]):
                                        if i+1==len(Name)-1:
                                            User = Mn.putName(Mn.getClears(Name[i+1][:len(Name[i+1])-1]))
                                        else:
                                             User = Mn.putName(Mn.getClears(Name[i+1]))
                                        F = 1
                                        break
                    if F == 0 :
                       return -1
                else:
                     User = Mn.putName(Mn.getClears(Name))
                for i in range(len(self.You.PlayersDK)):
                    if self.You.PlayersDK[i][0]==User:
                        return i
                T = [User,0,0]
                self.You.PlayersDK.append(T)
                return len(self.You.PlayersDK)-1
	def searchData(self):
		Mn = NormalNames()
		for i in range(len(self.Data)):
			if self.You.Name == Mn.getClears(self.Data[i][0]) and self.exceptDeath(self.Data[i][1])==1 and self.Data[i][2] != "taken" and self.Data[i][1][0]!="(" and self.Data[i][2] != "kicked" :
				self.You.Death+=1 
                                self.You.PlayersDK[self.checknick(self.Data[i],0)][1]+=1
                    
			elif self.searchNick(self.Data[i])==1 and Mn.getClears(self.Data[i][0])!="*" and Mn.getClears(self.Data[i][0])!="[pm]:":
				self.You.Kills+=1
                                self.You.PlayersDK[self.checknick(self.Data[i][0],1)][2]+=1
                               
			elif self.detectHitZone(self.Data[i]) >-1 and self.Data[i][0]=="You" and self.Data[i][1]=="hit":
				self.You.YourHitZone[self.detectHitZone(self.Data[i])]+=1
				self.You.YourHit+=1
                               
			elif self.Data[i][0]=="You" and self.Data[i][1]=="were":
				self.You.EmnyHitZone[self.detectHitZone(self.Data[i])]+=1
				self.You.EmnyHit+=1
                               
			elif Mn.getClears(self.Data[i][0])==self.You.Name and  self.detectFlagAction(self.Data[i])>-1:
				self.You.FlagAction[self.detectFlagAction(self.Data[i])]+=1
                               
                        elif self.You.Name == Mn.getClears(self.Data[i][0]) and  self.Data[i][1]=="joined":
                                if self.Data[i][3]=="red":
                                    self.You.Teams[0]+=1
                                if self.Data[i][3]=="blue":
                                    self.You.Teams[1]+=1
                               
               
pass

